import { saveIndPushDataTemp, saveIndPushDataTempBatch } from '@api/modules/indication'
import { cloneDeep } from 'lodash'
import { SAVING_STATUS } from '@utility/dict'

let savingTimer = null

export default {
  namespaced: true,
  state: () => ({
    savingStatus: SAVING_STATUS.init, // 省直履职能力指标填报和 绩效办共享指标推送 共用一个保存状态（不同的登录用户和不同的页面）
    indEvaOrgs: [], // 指标评价单位
    selectedOrgIds: {}, // 指标评价单位已选择单位
    schemeInfo: null, // 指标推送-当前考核方案信息
  }),
  getters: {
    savingStatus: state => state.savingStatus,
    indEvaOrgs: state => state.indEvaOrgs,
    schemeInfo: state => state.schemeInfo,
    selectedOrgIds: state => pid => {
      return state.selectedOrgIds[pid] || []
    },
  },
  actions: {
    saveIndicationPushData(context, { id, postData }) {
      context.commit('setSavingStatus', SAVING_STATUS.saving)
      saveIndPushDataTemp(id, postData)
        .then(res => {
          if (res.code === 0) {
            context.commit('setSavingStatus', SAVING_STATUS.success)
          } else {
            context.commit('setSavingStatus', SAVING_STATUS.failed)
          }
        })
        .catch(() => {
          context.commit('setSavingStatus', SAVING_STATUS.failed)
        })
    },
    saveIndicationTempBatch(context, { id, postData, isSecret }) {
      context.commit('setSavingStatus', SAVING_STATUS.saving)
      saveIndPushDataTempBatch(id, postData, isSecret)
        .then(res => {
          if (res.code === 0) {
            context.commit('setSavingStatus', SAVING_STATUS.success)
          } else {
            context.commit('setSavingStatus', SAVING_STATUS.failed)
          }
        })
        .catch(() => {
          context.commit('setSavingStatus', SAVING_STATUS.failed)
        })
    },
  },
  mutations: {
    setSavingStatus(state, status) {
      if (status === SAVING_STATUS.saving) {
        state.startSavingTime = new Date()
        if (savingTimer) {
          clearTimeout(savingTimer)
        }
        state.savingStatus = status
      } else {
        const savingGap = new Date() - state.startSavingTime
        if (state.startSavingTime && savingGap < 300) {
          savingTimer = setTimeout(() => {
            state.savingStatus = status
          }, 300 - savingGap)
        } else {
          state.savingStatus = status
        }
      }
    },
    setIndEvaOrgs(state, orgs) {
      state.indEvaOrgs = orgs || []
    },
    setSelectOrgs(state, { pid, orgId }) {
      const selectedOrgIds = cloneDeep(state.selectedOrgIds)
      let pIdOrgs = selectedOrgIds[pid]
      if (!pIdOrgs) {
        pIdOrgs = []
        selectedOrgIds[pid] = pIdOrgs
      }
      pIdOrgs.push(orgId)
      state.selectedOrgIds = selectedOrgIds
    },
    removeSelectOrgs(state, { pid, orgId }) {
      const selectedOrgIds = cloneDeep(state.selectedOrgIds)
      const pIdOrgs = selectedOrgIds[pid]
      const orgIdx = pIdOrgs.findIndex(item => item === orgId)
      if (orgIdx !== -1) {
        pIdOrgs.splice(orgIdx, 1)
      }
      state.selectedOrgIds = selectedOrgIds
    },
    clearSelectOrgs(state) {
      state.selectedOrgIds = {}
    },
    clearIndEvaOrgs(state) {
      state.indEvaOrgs = []
    },
    setShemeInfo(state, val) {
      state.schemeInfo = val
    },
  },
}
